package com.deer.wms.system.manage.test;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.StringUtils;
import java.util.HashMap;
import java.util.Map;

public class LedUtils {


    /**
     * LED控制接口
     */
    public static JsonResult ledControl(LedControlDto ledControlDto,String ip) {
        if (ObjectUtil.isEmpty(ledControlDto)) {
            throw new ServiceException(CommonCode.PARAMETER_ERROR, "请求参数不能为空");
        }
        //请求url
        String url = "http://" +ip + "/ledcontrol";
        String jsonString = JSONObject.toJSONString(ledControlDto);
        String result = HttpUtil.post(url, jsonString);
        JsonResult jsonResult = JSONObject.parseObject(result, JsonResult.class);
        return jsonResult;
    }

    /**
     * 获取料架 Mac地址和IP地址
     */
    public static MacAndIpResult getMacip(String ip) {
        //请求url
        String url = "http://"+ip+"/getmacip";
        String result = HttpUtil.get(url);
        MacAndIpResult macAndIpResult = JSONObject.parseObject(result, MacAndIpResult.class);
        return macAndIpResult;
    }


    /**
     * LED七色循环全亮(检查)
     */
    public static JsonResult ledCheck(String mac,String ip) {
        if (StringUtils.isEmpty(mac)) {
            throw new ServiceException(CommonCode.PARAMETER_ERROR, "请求参数不能为空");
        }
        //请求url
        String url = "http://"+ip+"/ledcheck";

        Map<String,String> map = new HashMap<>();
        map.put("mac",mac);

        String jsonString = JSONObject.toJSONString(map);
        String result = HttpUtil.post(url, jsonString);
        JsonResult jsonResult = JSONObject.parseObject(result, JsonResult.class);
        return jsonResult;
    }

    /**
     * 设置料架左右分隔数
     */
    public static JsonResult setMidNum(SetMidNumDto setmidnumDto,String ip) {
        if (ObjectUtil.isEmpty(setmidnumDto)) {
            throw new ServiceException(CommonCode.PARAMETER_ERROR, "请求参数不能为空");
        }
        //请求url
        String url = "http://"+ip+"/setmidnum";
        String jsonString = JSONObject.toJSONString(setmidnumDto);
        String result = HttpUtil.post(url, jsonString);
        JsonResult jsonResult = JSONObject.parseObject(result, JsonResult.class);
        return jsonResult;
    }

    /**
     * 设置心跳调用接口
     */
    public static JsonResult setServerIp(String ip) throws Exception {
        if (StringUtils.isEmpty(ip)) {
            throw new ServiceException(CommonCode.PARAMETER_ERROR, "请求参数不能为空");
        }
        //请求url
        String url = "http://"+ip+"/setserverip";

        //服务器ip
        String localIP = WebToolUtils.getLocalIP();
        String serviceUrl = "http://"+localIP+":8913/wms/led/heartbeat";
        Map<String,String> map = new HashMap<>();
        map.put("ip",serviceUrl);
        String jsonString = JSONObject.toJSONString(map);
        String result = HttpUtil.post(url, jsonString);
        JsonResult jsonResult = JSONObject.parseObject(result, JsonResult.class);
        return jsonResult;
    }


    /**
     * 设置料架库位总数
     */
    public static JsonResult setTotalNum(SetMidNumDto setmidnumDto,String ip) {
        if (ObjectUtil.isEmpty(setmidnumDto)) {
            throw new ServiceException(CommonCode.PARAMETER_ERROR, "请求参数不能为空");
        }
        //请求url
        String url = "http://"+ip+"/settotalnum";
        String jsonString = JSONObject.toJSONString(setmidnumDto);
        String result = HttpUtil.post(url, jsonString);
        JsonResult jsonResult = JSONObject.parseObject(result, JsonResult.class);
        return jsonResult;
    }

    /**
     * 全亮或全灭
     */
    public static JsonResult allLedControl (AllLedControlDto allLedControlDto,String ip) {
        if (ObjectUtil.isEmpty(allLedControlDto)) {
            throw new ServiceException(CommonCode.PARAMETER_ERROR, "请求参数不能为空");
        }
        //请求url
        String url = "http://"+ip+"/allLedControl";
        String jsonString = JSONObject.toJSONString(allLedControlDto);
        String result = HttpUtil.post(url, jsonString);
        JsonResult jsonResult = JSONObject.parseObject(result, JsonResult.class);
        return jsonResult;
    }


    /**
     * 授权
     * @param mac 控制设备的mac地址
     * @param key 密钥
     * @return
     */
    public static JsonResult settotalnum (String mac,String key,String ip) {
        if (StringUtils.isEmpty(mac) || StringUtils.isEmpty(key) ) {
            throw new ServiceException(CommonCode.PARAMETER_ERROR, "请求参数不能为空");
        }
        //请求url
        String url = "http://"+ip+"/allLedControl";

        //请求参数
        Map<String,String> map = new HashMap<>();
        map.put("mac",mac);
        map.put("key",key);
        String jsonString = JSONObject.toJSONString(map);

        String result = HttpUtil.post(url, jsonString);
        JsonResult jsonResult = JSONObject.parseObject(result, JsonResult.class);
        return jsonResult;
    }



}
